<?php

namespace ulit\thirdPayment;

use think\facade\Log;
use yzh52521\EasyHttp\Http;

class CanLangPay
{

    private static CanLangPay $instance;
    private string $baseUrl = 'https://api.changhuixin.cn/api/';

    private string $merchantId='208820241024396706'; //商户号208820240520788229  //208820241024396706



    private string $key ='30973b913eaf491ca66c5ff7d8342e0a';  //密钥389772207d914ce280f47cc65011bd2e //30973b913eaf491ca66c5ff7d8342e0a



    private function __construct()
    {
        // 可以在这里初始化一些资源或配置
    }

    // 私有克隆方法，防止外部克隆
    private function __clone()
    {
    }

    // 获取类的实例
    public static function getInstance(): CanLangPay
    {
        if (!isset(self::$instance)) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    /**
     *字段名    必选    类型    说明
     * merchantId    是    long    商户号
     * identityType    是    string    ALIPAY_LOGON_ID：记账本代付到支付宝账户 BANKCARD_ACCOUNT：记账本代付到卡
     * orderNo    是    string    订单号
     * amount    是    string    交易金额，单位：元，精确到小数点后两位
     * identity    是    string    银行卡号/支付宝账号
     * name    是    string    姓名
     * remark    否    string    转账备注
     * showName    否    string    记账本代付到户商家别名：true （固定值 为启用别名）
     * notifyUrl    是    string    回调地址
     * sign    是    string    签名数据
     * orderTitle    是    string    提款标题
     * accountType    是    string    支付宝通道： 1 到卡 ，0 到户
     * @param $orderNo
     * @param $amount
     * @param $identityType
     * @param $identity
     * @param $name
     * @param $remark
     * @param $showName
     * @param $notifyUrl
     * @param $orderTitle
     * @param $accountType
     * @return array|mixed
     */
    public function withdraw($orderNo, $amount, $identityType, $identity, $name, $remark,$showName, $notifyUrl, $orderTitle, $accountType)
    {

        $can_lang_pay=config('view.identity_type');
        $addressUrl=$this->baseUrl.'aliPay/transfer';
        $send['merchantId']=$this->merchantId;
        $send['identityType']=$can_lang_pay[$identityType];
        $send['orderNo']=$orderNo;
        $send['amount']=$amount;
        $send['identity']=$identity;
        $send['name']=$name;
        $send['remark']=$remark;
        $showName && $send['showName']=$showName;
        $send['notifyUrl']=$notifyUrl;
        $send['orderTitle']=$orderTitle;
        $send['accountType']=$accountType;
        $sign=$this->generateSign($send,$this->key);
        $send['sign']=$sign;
        $result=Http::asJson()->post($addressUrl,$send);
        $info=$result->array();
        Log::write(json_encode($result->array(),true),'log');
        if($info['code']==0){
            return $info['data'];
        }else{
              $arr['code']=400;
              $arr['msg']=$info['msg'];
             return $arr;
        }

    }


    public function query($orderNo)
    {
        $addressUrl=$this->baseUrl.'aliPay/transferQuery';
        $send['orderNo']=$orderNo;
        $send['merchantId']=$this->merchantId;
        $sign=$this->generateSign($send,$this->key);
        $send['sign']=$sign;
        $result=Http::asJson()->post($addressUrl,$send);
        $info=$result->array();
        if($info['code']==0){
            if($info['data']['code']==10000){
                $arr['code']=200;
                $arr['msg']=$info['data']['status'];
                $arr['data']=$info['data'];
            }else{


                $arr['code']=400;
                $arr['msg']= $info['data']['sub_msg'] ?? '失败';

            }

        }else{
            $arr['code']=400;
           $arr['msg']='失败';
        }
        return $arr;
    }

    public function moneyQuery()
    {
        $addressUrl=$this->baseUrl.'aliPay/moneyQuery';
        $send['merchantId']=$this->merchantId;
        $sign=$this->generateSign($send,$this->key);
        $send['sign']=$sign;
        $result=Http::asJson()->post($addressUrl,$send);

    }

    public function orderBill($orderNo)
    {
        $addressUrl=$this->baseUrl.'aliPay/orderBill';
        $send['merchantId']=$this->merchantId;
        $send['orderNo']=$orderNo;
        $sign=$this->generateSign($send,$this->key);
        $send['sign']=$sign;
        $result=Http::asJson()->post($addressUrl,$send);
        $info=$result->array();
        if($info['code']==0){
            if($info['data']['code']==10000){
                $arr['code']=200;
                $arr['msg']=$info['data']['status'];
                $arr['data']=$info['data'];
            }else{


                $arr['code']=400;
                $arr['msg']= $info['data']['sub_msg'] ?? '失败';

            }

        }else{
            $arr['code']=400;
            $arr['msg']='失败';
        }
        return $arr;
    }


    function generateSign($data, $merchantKey): string
    {
        // 对字典进行正序排序
        ksort($data);
        // 初始化连接字符串
        $stringToBeSigned = '';
        // 将key和value连接成字符串
        foreach ($data as $key => $value) {
            $stringToBeSigned .= $key . $value;
        }
        // 加入商户key
        $stringToBeSigned .= $merchantKey;
        // 使用MD5进行哈希计算
        $sign = md5($stringToBeSigned);
        // 将结果转换为小写
        return strtolower($sign);
    }

}